package com.aspire.wap.core.log;

import org.apache.commons.lang3.builder.ToStringStyle;

/**
 * 构造日志信息
 * 
 * @author huyunan 2009-9-13 上午09:52:04
 * 
 * @version 1.0
 * @deprecated
 */
public class LogMessageBuilder
{

	/**
	 * The default style of output to use.
	 */
	private static ToStringStyle defaultStyle = ToStringStyle.DEFAULT_STYLE;

	/**
	 * <p>
	 * Gets the default <code>ToStringStyle</code> to use.
	 * </p>
	 * 
	 * <p>
	 * This could allow the <code>ToStringStyle</code> to be controlled for an entire application with one call.
	 * </p>
	 * 
	 * <p>
	 * This might be used to have a verbose <code>ToStringStyle</code> during development and a compact <code>ToStringStyle</code> in production.
	 * </p>
	 * 
	 * @return the default <code>ToStringStyle</code>
	 */
	public static ToStringStyle getDefaultStyle()
	{
		return defaultStyle;
	}

	/**
	 * <p>
	 * Sets the default <code>ToStringStyle</code> to use.
	 * </p>
	 * 
	 * @param style
	 *            the default <code>ToStringStyle</code>
	 * @throws IllegalArgumentException
	 *             if the style is <code>null</code>
	 */
	public static void setDefaultStyle(ToStringStyle style)
	{
		if (style == null)
		{
			throw new IllegalArgumentException("The style must not be null");
		}
		defaultStyle = style;
	}

	/**
	 * Current toString buffer.
	 */
	private final StringBuffer buffer;
	/**
	 * The style of output to use.
	 */
	private final ToStringStyle style;

	public LogMessageBuilder()
	{
		this(getDefaultStyle(), null);
	}

	public LogMessageBuilder(ToStringStyle style)
	{
		this(style, null);
	}

	public LogMessageBuilder(ToStringStyle style, StringBuffer buffer)
	{
		if (style == null)
		{
			style = getDefaultStyle();
		}
		if (buffer == null)
		{
			buffer = new StringBuffer(512);
		}
		this.buffer = buffer;
		this.style = style;

		buffer.append("[");

	}

	// ----------------------------------------------------------------------------

	/**
	 * <p>
	 * Append to the <code>toString</code> an <code>Object</code> value.
	 * </p>
	 * 
	 * @param fieldName
	 *            the field name
	 * @param object
	 *            the value to add to the <code>toString</code>
	 * @return this
	 */
	public LogMessageBuilder append(String fieldName, Object object)
	{
		style.append(buffer, fieldName, object, null);
		return this;
	}

	// ----------------------------------------------------------------------------

	/**
	 * <p>
	 * Append the <code>toString</code> from the superclass.
	 * </p>
	 * 
	 * <p>
	 * This method assumes that the superclass uses the same <code>ToStringStyle</code> as this one.
	 * </p>
	 * 
	 * <p>
	 * If <code>superToString</code> is <code>null</code>, no change is made.
	 * </p>
	 * 
	 * @param superToString
	 *            the result of <code>super.toString()</code>
	 * @return this
	 * @since 2.0
	 */
	public LogMessageBuilder appendSuper(String superToString)
	{
		if (superToString != null)
		{
			style.appendSuper(buffer, superToString);
		}
		return this;
	}

	/**
	 * <p>
	 * Append the <code>toString</code> from another object.
	 * </p>
	 * 
	 * <p>
	 * This method is useful where a class delegates most of the implementation of its properties to another class. You can then call <code>toString()</code> on the other class and pass the result
	 * into this method.
	 * </p>
	 * 
	 * <pre>
	 * private AnotherObject delegate;
	 * private String fieldInThisClass;
	 * 
	 * public String toString()
	 * {
	 * 	return new ToStringBuilder(this).appendToString(delegate.toString())
	 * 			.append(fieldInThisClass).toString();
	 * }
	 * </pre>
	 * 
	 * <p>
	 * This method assumes that the other object uses the same <code>ToStringStyle</code> as this one.
	 * </p>
	 * 
	 * <p>
	 * If the <code>toString</code> is <code>null</code>, no change is made.
	 * </p>
	 * 
	 * @param toString
	 *            the result of <code>toString()</code> on another object
	 * @return this
	 * @since 2.0
	 */
	public LogMessageBuilder appendToString(String toString)
	{
		if (toString != null)
		{
			style.appendToString(buffer, toString);
		}
		return this;
	}

	/**
	 * <p>
	 * Gets the <code>StringBuffer</code> being populated.
	 * </p>
	 * 
	 * @return the <code>StringBuffer</code> being populated
	 */
	public StringBuffer getStringBuffer()
	{
		return buffer;
	}

	// ----------------------------------------------------------------------------

	/**
	 * <p>
	 * Gets the <code>ToStringStyle</code> being used.
	 * </p>
	 * 
	 * @return the <code>ToStringStyle</code> being used
	 * @since 2.0
	 */
	public ToStringStyle getStyle()
	{
		return style;
	}

	/**
	 * <p>
	 * Returns the built <code>toString</code>.
	 * </p>
	 * 
	 * <p>
	 * This method appends the end of data indicator, and can only be called once. Use {@link #getStringBuffer} to get the current string state.
	 * </p>
	 * 
	 * @return the String <code>toString</code>
	 */
	@Override
	public String toString()
	{
		style.appendEnd(buffer, null);
		return buffer.toString();
	}

}